package view_alertwebhook

import (
	"bigdevops/src/common"
	"bigdevops/src/config"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/prometheus/alertmanager/notify/webhook"
	"github.com/prometheus/alertmanager/template"
	"go.uber.org/zap"
)

// 要求接口 尽可能快的返回 ，要求它异步处理
func AlertReceive(c *gin.Context) {

	sc := c.MustGet(common.GIN_CTX_CONFIG_CONFIG).(*config.AlertWebhookConfig)
	alertReceiveQ := c.MustGet(common.GIN_CTX_CONFIG_ALERTRECEIVEQ).(chan template.Alert)
	var msg webhook.Message
	if err := c.BindJSON(&msg); err != nil {
		msg := fmt.Sprintf("解析alertmanager传过来的alert-json错误:%v", err.Error())
		sc.Logger.Error(msg,
			zap.Error(err),
		)
		common.ReqBadFailWithMessage(msg, c)
		return
	}
	//baseMsg := fmt.Sprintf("[状态：%s][报警条数:%d]", msg.Status, len(msg.Alerts))

	sc.Logger.Info("接收alertmanager的告警打印",
		zap.Any("状态", msg.Status),
		zap.Any("报警条数", len(msg.Alerts)),
	)
	for i := 0; i < len(msg.Alerts); i++ {
		alert := msg.Alerts[i]

		sc.Logger.Debug("接收alertmanager告警，打印详情",
			zap.Any(fmt.Sprintf("Receiver:%v[%d/%d]", msg.Receiver, i+1, len(msg.Alerts)), alert),
		)
		alertReceiveQ <- alert

	}
	c.JSON(200, "ok")
}
